package com.lhy.wanandroid.ui.main.home

import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.gyf.immersionbar.ImmersionBar
import com.lhy.demo.R
import com.lhy.demo.http.glide.GlideApp
import com.lhy.demo.ui.fragment.TestFragmentB
import com.lhy.demo.ui.fragment.TestFragmentC
import com.lhy.demo.ui.fragment.TestFragmentD
import com.lhy.demo.widget.XCollapsingToolbarLayout
import com.lhy.demo.widget.XCollapsingToolbarLayout.OnScrimsListener
import com.lhy.wanandroid.base.MyBaseVmFragment
import com.xiaojianjun.wanandroid.model.bean.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_home.*

class  HomeFragment : MyBaseVmFragment<HomeVM>(), OnScrimsListener {

    private val bannerList= mutableListOf<Banner>()

    override fun viewModelClass(): Class<HomeVM> =HomeVM::class.java

    private lateinit var fragments: List<Fragment>

    override fun getLayoutId(): Int = R.layout.fragment_home

    companion object{
        fun  newInstance(): HomeFragment {
            return HomeFragment()
        }
    }



    override fun initView() {
        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐

        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
        ImmersionBar.setTitleBar(attachActivity, t_test_title)

        //设置渐变监听

        //设置渐变监听
        ctl_test_bar.setOnScrimsListener(this)

        banner.addBannerLifecycleObserver(this)
                .setAdapter(object : BannerImageAdapter<Banner>(bannerList){
                    override fun onBindView(holder: BannerImageHolder, data: Banner, position: Int, size: Int) {
                        GlideApp.with(this@HomeFragment)
                                .load(data.imagePath).into(holder.imageView)
                    }

                }).setIndicator(CircleIndicator(this.attachActivity))

        fragments = listOf(
                TestFragmentD.newInstance(),
                TestFragmentB.newInstance(),
                TestFragmentC.newInstance(),
                TestFragmentD.newInstance(),
                TestFragmentC.newInstance()
        )
        val titles = listOf<CharSequence>(
                getString(R.string.popular_articles),
                getString(R.string.latest_project),
                getString(
                        R.string.plaza
                ),
                getString(R.string.project_category),
                getString(R.string.wechat_public)
        )
        viewPager.adapter = SimpleFragmentPagerAdapter(childFragmentManager, fragments, titles)
        viewPager.offscreenPageLimit = fragments.size
        tabLayout.setupWithViewPager(viewPager)

    }

    override fun initData() {
        //获取轮播图数据
        mViewModel.getBannerList()

    }

    override fun observe() {
        super.observe()
        mViewModel.bannerDataList.observe(this, Observer {
            bannerList.clear()
            bannerList.addAll(it)
            banner.adapter.notifyDataSetChanged()
        })
    }

    override fun isStatusBarEnabled(): Boolean {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled()
    }

    override fun statusBarDarkFont(): Boolean {
        return ctl_test_bar.isScrimsShown()
    }

    /**
     * CollapsingToolbarLayout 渐变回调
     *
     * [XCollapsingToolbarLayout.OnScrimsListener]
     */
    override fun onScrimsStateChange(layout: XCollapsingToolbarLayout?, shown: Boolean) {
        if (shown) {
            tv_test_hint.setBackgroundResource(R.drawable.bg_home_search_bar_gray)
            tv_test_hint.setTextColor(ContextCompat.getColor(attachActivity, R.color.black60))
            getStatusBarConfig()!!.statusBarDarkFont(true).init()
        } else {
            tv_test_hint.setBackgroundResource(R.drawable.bg_home_search_bar_transparent)
            tv_test_hint.setTextColor(ContextCompat.getColor(attachActivity, R.color.white60))
            getStatusBarConfig()!!.statusBarDarkFont(false).init()
        }
    }
}